package com.shuhe.domain.mxgc;

import com.alibaba.fastjson.JSONObject;
import com.shuhe.metastore.MetaMysql;
import com.shuhe.metastore.Neo4jDB;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Date;

import static com.shuhe.common.CommonFunction.closeDb;
import static com.shuhe.common.Tools.dateFormat;


public class TaskGraphExecute implements Runnable {

    JSONObject jsonObject;
    String uuid;

    public TaskGraphExecute(JSONObject jsonObject, String uuid) {
        this.jsonObject = jsonObject;
        this.uuid = uuid;
    }

    public void run() {
        Connection metaConn = null;
        String sql = "";
        try {

            metaConn = MetaMysql.connect();
            String table_name = "";
            sql = "select task_id,task_name,a.table_name from graph_node_relation a,v_mxgc_task_list b where a.id=b.mx_id " +
                    "and b.rwfl='graph' and task_id='" + jsonObject.get("task_id") + "' ";
            ResultSet rs = MetaMysql.querySql(metaConn, sql);
            if (rs.next()) {
                table_name = rs.getString("table_name");
            }
            sql = "insert into mxgc_user_task_log(task_pc,task_id,task_name,task_rule,qssj,zzsj,status,sjl) values" +
                    "('" + uuid + "','" + jsonObject.get("task_id") + "','" + jsonObject.get("task_name") + "','runImmedia','" +
                    dateFormat(new Date()) + "','','','')";
            MetaMysql.execSql(metaConn, sql);
            Neo4jDB.makeData(table_name);
            sql = "update mxgc_user_task_log set zzsj='" + dateFormat(new Date()) + "', status='success' where task_pc='"
                    + uuid + "' and task_id='" + jsonObject.get("task_id") + "'";
            MetaMysql.execSql(metaConn, sql);


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeDb(metaConn);
        }


    }
}
